#include "LinkStack.h"

//1，初始化函数
LinkStack* Init_LinkStack() {
    LinkStack* stack = (LinkStack*)malloc(sizeof(LinkStack));
    stack->head.next = NULL;
    stack->size = 0;
    return stack;
}
//2，入栈
void Push_LinkStack(LinkStack* stack,LinkNode* data) {
    if(stack == NULL || data == NULL) return;

    data->next = stack->head.next;
    stack->head.next = data;
    stack->size++;
}
//3，出栈
LinkNode* Pop_LinkStack(LinkStack* stack) {
    if(stack == NULL) return  NULL;
    if(stack->size == 0) return NULL;

    LinkNode* save = stack->head.next;
    stack->head.next = save->next;
    save->next = NULL;

    stack->size--;
    return save;
}
//4，返回栈顶元素
LinkNode* Top_LinkStack(LinkStack* stack) {
    if(stack == NULL) return  NULL;
    if(stack->size == 0) return NULL;

    return stack->head.next;
}
//5，返回栈元素的个数
int Size_LinkStack(LinkStack* stack) {
    if(stack == NULL) return  -1;
    return stack->size;
}
//6，清空栈
void Clear_LinkStack(LinkStack* stack) {
    if(stack == NULL) return;

    stack->head.next = NULL;
    stack->size = 0;

}
//7，销毁栈
void FreeSpace_LinkStack(LinkStack* stack) {
    if(stack == NULL) return;

    free(stack);
}